
package com.talking.model;

import com.talking.inte.DownloadStatus;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Http {
    public static final String BASE_URL = "http://192.168.1.9/";
    public static Request.Builder getBuilder(String url){
        return new Request.Builder().url(url).addHeader("Content-Type","application/json").addHeader("Access-Control-Allow-Origin","*");
    }
    public static OkHttpClient getClient(){
        return new OkHttpClient.Builder().readTimeout(1000, TimeUnit.SECONDS).connectTimeout(1000, TimeUnit.SECONDS).writeTimeout(1000,TimeUnit.SECONDS).build();
    }
    public static void get(String url, Callback callback){
        getClient().newCall(getBuilder(BASE_URL+url).build()).enqueue(callback);
    }
    public static void post(JSONObject jsonObject,String url,Callback callback){
        getClient().newCall(getBuilder(BASE_URL+url).post(RequestBody.create(MediaType.parse("application/json;charset=utf-8"), String.valueOf(jsonObject))).build()).enqueue(callback);
    }
    public static Request.Builder getFileBuilder(String url){
        return new Request.Builder().url(url);
    }
    public static void upload(String url,File file,Callback callback){
        getClient().newCall(getFileBuilder(BASE_URL+url).post(new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file)).build()).build()).enqueue(callback);
    }
    public static void uploadFiles(String url, List<String> files, Callback callback){
        if (files.size()>0) {
            MultipartBody.Builder body = new MultipartBody.Builder();
            body.setType(MultipartBody.FORM);
            for (String dir : files) {
                File file = new File(dir);
                body.addFormDataPart(file.getName(), file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
            }
            getClient().newCall(getFileBuilder(BASE_URL + url).post(body.build()).build()).enqueue(callback);
        }else {
            get(url,callback);
        }
    }
//    public static void downLoadFile(String url, String dir, DownloadStatus downloadStatus) {
//        new OkHttpClient().newCall(new Request.Builder().url(url).build()).enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                downloadStatus.error(e);
//            }
//            @Override
//            public void onResponse(@NotNull Call call, @NotNull Response response) {
//                File file = new File(dir);
//                OutputStream outputStream = null;
//                InputStream inputStream = response.body().byteStream();
//                try {
//                    outputStream = new FileOutputStream(file);
//                    int len = 0;
//                    byte[] buffer = new byte[1024*10];
//                    while ((len = inputStream.read(buffer))!=-1){
//                        outputStream.write(buffer,0,len);
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                } finally {
//                    try {
//                        inputStream.close();
//                        if(outputStream != null)
//                            outputStream.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                    downloadStatus.finish(dir);
//                }
//            }
//        });
//    }
}